package com.mcptt.db.model;

import android.content.ContentValues;
import android.database.Cursor;
import android.database.DatabaseUtils;

import com.cloudptt.api.comm.CheckData;
import com.mcptt.db.DbConst;

public class HistoryInfo
{
	public static String THIS_FILE = "HistoryInfo";
	private AddrInfo peerAddrInfo = null;
	private AddrInfo lastSender = null;
	private int lastContentType = SessionInfo.ContentType_Text;//消息类型
	private String lastMsgContent;//消息内容（包含发送用户名名）
	private long lastMsgDate;   //接受时间
	private int unreadCount = 1;  //消息个数

	public HistoryInfo(AddrInfo receiver)
	{
		this.peerAddrInfo = receiver;
	}

	public HistoryInfo(Cursor c)
	{
		ContentValues args = new ContentValues();
		DatabaseUtils.cursorRowToContentValues(c, args);
		createFromContentValue(args);
	}

	public final void createFromContentValue(ContentValues args)
	{
		peerAddrInfo = new AddrInfo();
		String tmp_s;
		tmp_s = args.getAsString(DbConst.COL_PeerType);
		if(tmp_s != null) {
			int value = CheckData.parseInt(tmp_s);
			peerAddrInfo.setType(value);
		}
		tmp_s = args.getAsString(DbConst.COL_PeerAddrId);
		if(tmp_s != null) {
			peerAddrInfo.setAddrId(tmp_s);
		}
		tmp_s = args.getAsString(DbConst.COL_PeerName);
		if(tmp_s != null) {
			peerAddrInfo.setName(tmp_s);
		}
		tmp_s = args.getAsString(DbConst.COL_Type);
		if(tmp_s != null)
		{
			int value = CheckData.parseInt(tmp_s);
			if(value != -1)
			{
				peerAddrInfo.setType(value);
			}
		}
		tmp_s = args.getAsString(DbConst.COL_Name);
		if(tmp_s != null) {
			peerAddrInfo.setName(tmp_s);
		}
		tmp_s = args.getAsString(DbConst.COL_Attr);
		if(tmp_s != null)
		{
			int value = CheckData.parseInt(tmp_s);
			peerAddrInfo.setAttr(value);
		}

		lastSender = new AddrInfo();
		lastSender.setType(UserInfo.UserType_User);
		tmp_s = args.getAsString(DbConst.COL_LastSenderId);
		if(tmp_s != null) {
			lastSender.setAddrId(tmp_s);
		}

		tmp_s = args.getAsString(DbConst.COL_LastSenderName);
		if(tmp_s != null) {
			lastSender.setName(tmp_s);
		}

		tmp_s = args.getAsString(DbConst.COL_LastMsgType);
		if(tmp_s != null)
		{
			int value = CheckData.parseInt(tmp_s);
			if(value != -1)
			{
				lastContentType = value;
			}
			else
			{
				lastContentType = SessionInfo.ContentType_Text;
			}
		}

		tmp_s = args.getAsString(DbConst.COL_LastMsgContent);
		if(tmp_s != null) {
			lastMsgContent = tmp_s;
		}
		lastMsgDate = args.getAsLong(DbConst.COL_LastMsgDate);
		tmp_s = args.getAsString(DbConst.COL_UnreadCount);
		if(tmp_s != null)
		{
			int value = CheckData.parseInt(tmp_s);
			if(value != -1)
			{
				unreadCount = value;
			}
		}

	}
	public static String[] getColumns() {
		return new String[]{DbConst.COL_PeerAddrId,DbConst.COL_LastSenderId,DbConst.COL_LastSenderName,DbConst.COL_LastMsgType,
				DbConst.COL_LastMsgContent,DbConst.COL_LastMsgDate,DbConst.COL_UnreadCount,DbConst.COL_Attr};
	}

	/**
	 * Pack the object content value to store
	 *
	 * @return The content value representing the message
	 */
	public ContentValues getContentValues() {
		ContentValues cv = new ContentValues();
		cv.put(DbConst.COL_PeerType, peerAddrInfo.getType());
		cv.put(DbConst.COL_PeerAddrId, peerAddrInfo.getAddrId());
		cv.put(DbConst.COL_PeerName, peerAddrInfo.getName());
		cv.put(DbConst.COL_LastSenderId, lastSender.getAddrId());
		cv.put(DbConst.COL_LastSenderName, lastSender.getName());
		cv.put(DbConst.COL_LastMsgType, lastContentType);
		cv.put(DbConst.COL_LastMsgContent, lastMsgContent);
		cv.put(DbConst.COL_LastMsgDate, lastMsgDate);
		cv.put(DbConst.COL_UnreadCount, unreadCount);
		return cv;
	}
	public ContentValues getUpdateContentValues() {
		ContentValues cv = new ContentValues();
//		cv.put(DbConst.COL_FromId, fromAddrInfo.getMcId());
		cv.put(DbConst.COL_LastSenderId, lastSender.getAddrId());
		cv.put(DbConst.COL_LastSenderName, lastSender.getName());
		cv.put(DbConst.COL_LastMsgType, lastContentType);
		cv.put(DbConst.COL_LastMsgContent, lastMsgContent);
		cv.put(DbConst.COL_LastMsgDate, lastMsgDate);
		cv.put(DbConst.COL_UnreadCount, unreadCount);
		return cv;
	}
	public ContentValues getUpdateContentValuesExceptUnreadCount() {
		ContentValues cv = new ContentValues();
//		cv.put(DbConst.COL_FromId, fromAddrInfo.getMcId());
		cv.put(DbConst.COL_LastSenderId, lastSender.getAddrId());
		cv.put(DbConst.COL_LastSenderName, lastSender.getName());
		cv.put(DbConst.COL_LastMsgType, lastContentType);
		cv.put(DbConst.COL_LastMsgContent, lastMsgContent);
		cv.put(DbConst.COL_LastMsgDate, lastMsgDate);
//		cv.put(DbConst.COL_UnreadCount, unreadCount);
		return cv;
	}

	public AddrInfo getPeerAddrInfo() {
		return peerAddrInfo;
	}
	public AddrInfo getLastSender() {
		return lastSender;
	}
	public void setLastSender(AddrInfo lastSender) {
		this.lastSender = lastSender;
	}
	public String getLastMsgContent() {
		return lastMsgContent;
	}

	public void setLastMsgContent(String lastMsgContent) {
		this.lastMsgContent = lastMsgContent;
	}

	public long getLastMsgDate() {
		return lastMsgDate;
	}

	public void setLastMsgDate(long lastMsgDate) {
		this.lastMsgDate = lastMsgDate;
	}

	public String getDscp()
	{
		return peerAddrInfo.getDscp();
	}

	public void setUnreadCount(int unreadCount) {
		this.unreadCount = unreadCount;
	}
	public int getUnreadCount() {
		return unreadCount;
	}

	public int getLastContentType()
	{
		return lastContentType;
	}

	public void setLastContentType(int lastContentType)
	{
		this.lastContentType = lastContentType;
	}

//	public boolean isAffiliation()
//	{
//		return isAffiliation;
//	}

	public String toString()
	{
		StringBuffer buffer = new StringBuffer("HistoryInfo:");
		buffer.append("receiver = ").append(peerAddrInfo).append(",");
		buffer.append("lastSender = ").append(lastSender).append(",");
		buffer.append("lastMsgType = ").append(lastContentType).append(",");
		buffer.append("lastMsgContent = ").append(lastMsgContent).append(",");
		buffer.append("lastMsgDate = ").append(lastMsgDate).append(",");
		buffer.append("unreadCount = ").append(unreadCount).append(";");
//		buffer.append("isAffiliation = ").append(isAffiliation).append(";");

		return buffer.toString();
	}
}
